package com.wtwd.campus.jdzc.service.impl;

import com.wtwd.campus.entity.School;
import com.wtwd.campus.entity.Teacher;
import com.wtwd.campus.jdzc.dao.JdzcStudentMapper;
import com.wtwd.campus.jdzc.dao.JdzcUserStudentPermissionMapper;
import com.wtwd.campus.jdzc.entity.DeviceInfo;
import com.wtwd.campus.jdzc.entity.JdzcUserStudentPermission;
import com.wtwd.campus.jdzc.service.AuthService;
import com.wtwd.campus.jdzc.service.JdzcClassService;
import com.wtwd.campus.jdzc.service.JdzcTeacherService;
import com.wtwd.campus.service.SchoolService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author mjy
 * @date 2022/02/15
 * @description
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private JdzcTeacherService teacherService;

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private JdzcStudentMapper jdzcStudentMapper;

    @Autowired
    private JdzcUserStudentPermissionMapper jdzcUserStudentPermissionMapper;

    @Autowired
    private JdzcClassService jdzcClassService;

    @Override
    public Boolean authTeacher(String phone, String jdzcOrgCode) {

        Teacher teacher = teacherService.getTeacherByPhone(phone);
        if (teacher == null) {
            return false;
        }
        School school = schoolService.getSchoolInfoById(teacher.getSchoolId());
        if (school != null) {
            if (school.getOrgCode().startsWith(jdzcOrgCode)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean authDevice(DeviceInfo deviceInfo, String jdzcOrgCode) {
        if (deviceInfo == null) {
            return false;
        }
        if (deviceInfo.getOrgCode().startsWith(jdzcOrgCode)) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean authStudent(Integer studentId, String jdzcOrgCode, Integer userId) {
        List<Integer> IdList = new ArrayList<>();
        IdList.add(studentId);
        List<Map<String, Object>> schoolOrgCodeList = jdzcStudentMapper.getSchoolOrgCodeListByStuIdList(IdList);
        if (schoolOrgCodeList == null || schoolOrgCodeList.size() == 0) {
            return false;
        }
        Map<String, Object> map = schoolOrgCodeList.get(0);
        //该学生的学校是否存在于该组织
        String orgCode = (String) map.get("org_code");
        if (StringUtils.isBlank(orgCode)) {
            return false;
        }
        if (orgCode.startsWith(jdzcOrgCode)) {
            //校验该学生是否属于该用户
            JdzcUserStudentPermission jdzcUserStudentPermission = jdzcUserStudentPermissionMapper.getWxUserStudentPermissionByCondition(studentId, userId);
            if (jdzcUserStudentPermission != null) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    @Override
    public Boolean authStudentOrgCode(Integer stuId, String jdzcOrgCode, Integer userId) {
        List<Integer> IdList = new ArrayList<>();
        IdList.add(stuId);
        List<Map<String, Object>> schoolOrgCodeList = jdzcStudentMapper.getSchoolOrgCodeListByStuIdList(IdList);
        if (schoolOrgCodeList == null || schoolOrgCodeList.size() == 0) {
            return false;
        }
        Map<String, Object> map = schoolOrgCodeList.get(0);
        //该学生的学校是否存在于该组织
        String orgCode = (String) map.get("org_code");
        if (StringUtils.isBlank(orgCode)) {
            return false;
        }
        if (orgCode.startsWith(jdzcOrgCode)) {
            //校验该学生是否属于该用户
            return true;
        }
        return false;
    }

    @Override
    public Boolean authClassAndTeacher(String phone, Integer classId) {
        Integer classesId = jdzcClassService.getClassIdByClassId(classId);
        if (classesId == null) {
            return false;
        }
        Teacher teacher = teacherService.getTeacherByPhone(phone);
        if (teacher == null) {
            return false;
        }
        String teachClass = teacher.getTeachClass();
        if (StringUtils.isBlank(teachClass)){
            return false;
        }
        String[] classList = teachClass.split(",");
        for (String s : classList) {
            Integer sclassId = Integer.valueOf(s);
            if (sclassId.equals(classesId)){
                return true;
            }
        }
        return false;
    }


}
